﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace CreateAtmDb
{
    public class Program
    {
        private static Random random = new Random();

        public static void CreateCardAccountsTable(SqlConnection dbCon)
        {
            SqlCommand createTableCommand = new SqlCommand();
            createTableCommand.Connection = dbCon;
            createTableCommand.CommandText = 
                "CREATE TABLE CardAccounts( " +
                "Id int IDENTITY PRIMARY KEY, " +
                "CardNumber char(10) UNIQUE NOT NULL, " +
                "CardPIN char(4) NOT NULL, " +
                "CardCash money NOT NULL)";
            createTableCommand.ExecuteNonQuery();
        }

        public static void AddRecordsToAccounts(SqlConnection dbCon, int recordsAmount)
        {
            SqlCommand addRecord = new SqlCommand();
            addRecord.Connection = dbCon;
            addRecord.CommandText =
                "INSERT INTO CardAccounts( " + 
                "CardNumber, CardPIN, CardCash) " + 
                "VALUES (@CardNumber, @CardPIN, @CardCash)";
            addRecord.Parameters.Add("@CardNumber", null);
            addRecord.Parameters.Add("@CardPIN", null);
            addRecord.Parameters.Add("@CardCash", null);
            for (int i = 0; i < recordsAmount; i++)
			{
                addRecord.Parameters["@CardNumber"] = new SqlParameter(
                    "@CardNumber", random.Next(1000000000, int.MaxValue));
                addRecord.Parameters["@CardPIN"] = new SqlParameter(
                    "@CardPIN", random.Next(1000, 10000));
                addRecord.Parameters["@CardCash"] = new SqlParameter(
                    "@CardCash", random.Next(0, 1000000));
                addRecord.ExecuteNonQuery();
			}
        }

        public static void Main()
        {
            SqlConnection dbCon = new SqlConnection("Server=.\\SQLEXPRESS; " +
                "Database=atm; Integrated Security=true");
            dbCon.Open();
            using (dbCon)
            {
                AddRecordsToAccounts(dbCon, 10);
            }
        }
    }
}
